package Wifichat.namespace;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TextView;

public class ChatAdapter extends BaseAdapter{
	private ArrayList<ChatEntity> list;
	private Context ctx;
	private Map<Integer, View> rowViews = new HashMap<Integer, View>();
	
	public ChatAdapter(Context context ,ArrayList<ChatEntity> list) {
		ctx = context;
		this.list = list;
	}
	
	public boolean areAllItemsEnabled() {
		return false;
	}
	public boolean isEnabled(int arg0) {
		return false;
	}
	public int getCount() {
		return list.size();
	}
	public Object getItem(int position) {
		return list.get(position);
	}
	public long getItemId(int position) {
		return position;
	}
	public int getItemViewType(int position) {
		return position;
	}
	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView = rowViews.get(position);
		if (rowView == null) {
			ChatEntity entity = list.get(position);
			
			int itemLayout = entity.getLayoutID();
			
			LinearLayout layout = new LinearLayout(ctx);
			LayoutInflater vi = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			vi.inflate(itemLayout, layout,true);
			
			TextView txvName = (TextView) layout.findViewById(R.id.txvName);
			txvName.setText(entity.getName());
			
			TextView txvText = (TextView) layout.findViewById(R.id.txvInfo);
			txvText.setText(entity.getInfo());
			rowViews.put(position, layout);
			return layout;
		}
		return rowView;
	}
	


	public void addItem(ChatEntity item)
	{
		list.add(item);
		//this.notifyDataSetInvalidated();
		this.notifyDataSetChanged();
	}

	
	
}
